package com.example.chatroom.controller;

import com.example.chatroom.common.errorcode.ControllerErrorCodeConstants;
import com.example.chatroom.common.exception.ControllerException;
import com.example.chatroom.common.pojo.CommonResult;
import com.example.chatroom.common.utils.JWTUtil;
import com.example.chatroom.controller.param.AddApplyFriendParam;
import com.example.chatroom.controller.param.GetUsersParam;
import com.example.chatroom.controller.param.HandlerApplyFriendParam;
import com.example.chatroom.controller.result.GetApplyFriendsResult;
import com.example.chatroom.controller.result.GetUsersResult;
import com.example.chatroom.service.IApplyFriendService;
import com.example.chatroom.service.dto.GetApplyFriendsDTO;
import com.example.chatroom.service.dto.GetUsersDTO;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/applyfriend")
public class ApplyFriendController {
    @Autowired
    private JWTUtil jwtUtil;
    @Autowired
    private IApplyFriendService applyFriendService;
    @PostMapping("/getUsers")
    public CommonResult<GetUsersResult>getUsers(
            @Validated @RequestBody GetUsersParam getUsersParam,
            HttpServletRequest httpServletRequest){
        log.info("getUsers getUsersParam:{}",getUsersParam);
        Long userId = jwtUtil.getUserId(httpServletRequest.getHeader("user_token"));
        GetUsersDTO getUsersDTO=applyFriendService.getUsers(getUsersParam,userId);
        return CommonResult.success(convertToGetUserResult(getUsersDTO));
    }

    private GetUsersResult convertToGetUserResult(GetUsersDTO getUsersDTO) {
        if(getUsersDTO==null){
            log.error(ControllerErrorCodeConstants.APPLY_FRIEND_GET_USERS_ERROR.getMsg());
            throw new ControllerException(ControllerErrorCodeConstants.APPLY_FRIEND_GET_USERS_ERROR);
        }
        GetUsersResult getUsersResult=new GetUsersResult();
        getUsersResult.setUserInfoList(
        getUsersDTO.getUserInfoList().stream().map(userInfo -> {
            GetUsersResult.UserInfo userInfo1=new GetUsersResult.UserInfo();
            userInfo1.setUserId(userInfo.getUserId());
            userInfo1.setUserName(userInfo.getUserName());
            return userInfo1;
        }).collect(Collectors.toList()));
        return getUsersResult;
    }
    @PostMapping("/add")
    public CommonResult<Long>addInfo(@RequestBody @Validated AddApplyFriendParam addApplyFriendParam){
        log.info("addInfo AddApplyFriendParam:{}",addApplyFriendParam);
        Long id=applyFriendService.addInfo(addApplyFriendParam);
        return CommonResult.success(id);
    }
    @GetMapping("/getApplyFriends")
    public CommonResult<List<GetApplyFriendsResult>>getApplyFriends(HttpServletRequest httpServletRequest){
        Long userId = jwtUtil.getUserId(httpServletRequest.getHeader("user_token"));
        List<GetApplyFriendsDTO>getApplyFriendsDTOS=applyFriendService.getApplyFriends(userId);
        return CommonResult.success(convertToGetApplyFriendsResult(getApplyFriendsDTOS));
    }

    private List<GetApplyFriendsResult> convertToGetApplyFriendsResult(List<GetApplyFriendsDTO> getApplyFriendsDTOS) {
        return getApplyFriendsDTOS.stream().map(getApplyFriendsDTO -> {
            GetApplyFriendsResult getApplyFriendsResult=new GetApplyFriendsResult();
            getApplyFriendsResult.setFormName(getApplyFriendsDTO.getFormName());
            getApplyFriendsResult.setApplyContent(getApplyFriendsDTO.getApplyContent());
            getApplyFriendsResult.setPostTime(getApplyFriendsDTO.getGmtCreate());
            getApplyFriendsResult.setId(getApplyFriendsDTO.getId());
            return getApplyFriendsResult;
        }).collect(Collectors.toList());
    }
    @PostMapping("/handleApplyFriend")
    public CommonResult<Boolean>handlerApplyFriend(@Validated @RequestBody HandlerApplyFriendParam handlerApplyFriendParam){
        log.info("handlerApplyFriend HandlerApplyFriendParam:{}",handlerApplyFriendParam);
        applyFriendService.handlerApplyFriend(handlerApplyFriendParam);
        return CommonResult.success(true);
    }
}
